#include "GY_Button.h"
#include <QDebug>
#include <QGraphicsSceneMouseEvent>
#include "Graphics/GY_GraphicsReflection.h"

GY_Button::GY_Button(const QString &guid, QGraphicsItem *parent)
    : GY_ContainerTransform(guid,this, parent)
{
    initializationPropertyInfo();
    connect(graphicsPathItem,&GY_GraphicsButtonEditItem::doubleClicked,
            this,&GY_Button::recvDoubleClicked);

    int state = m_PropertyInfoMap["CurrentState"].propertyValue.value<int>();
    m_pTransFormGroup = new GY_TransformGroup(QString("Group%1").arg(state));
    m_ChildrensMap.insert(m_pTransFormGroup->getGuid(),m_pTransFormGroup);//在已包含的图形映射中插入（注册）新添加的图形
    this->addToGroup(m_pTransFormGroup);

    qreal leftActiveArea = m_PropertyInfoMap["ActiveArea.Left"].propertyValue.value<qreal>();
    qreal bottomActiveArea = m_PropertyInfoMap["ActiveArea.Bottom"].propertyValue.value<qreal>();
    qreal rightActiveArea = m_PropertyInfoMap["ActiveArea.Right"].propertyValue.value<qreal>();
    qreal topActiveArea = m_PropertyInfoMap["ActiveArea.Top"].propertyValue.value<qreal>();

    QPointF topRightPoint = m_PropertyInfoMap["TopRightPoint"].propertyValue.value<QPointF>();
    QPointF leftPoint = m_PropertyInfoMap["LeftPoint"].propertyValue.value<QPointF>();
    QPointF bottomPoint = m_PropertyInfoMap["BottomPoint"].propertyValue.value<QPointF>();
    QPointF rightPoint = m_PropertyInfoMap["RightPoint"].propertyValue.value<QPointF>();
    QPointF topPoint = m_PropertyInfoMap["TopPoint"].propertyValue.value<QPointF>();

    leftActiveArea = leftPoint.x();
    bottomActiveArea = bottomPoint.y();
    rightActiveArea = rightPoint.x();
    topActiveArea = topPoint.y();

    m_PropertyInfoMap["ActiveArea.Left"].propertyValue = leftActiveArea;
    m_PropertyInfoMap["ActiveArea.Bottom"].propertyValue = bottomActiveArea;
    m_PropertyInfoMap["ActiveArea.Right"].propertyValue = rightActiveArea;
    m_PropertyInfoMap["ActiveArea.Top"].propertyValue = topActiveArea;

    QMap<QVariant,QPointF> editPoints =
    {
        {"TopRightPoint",topRightPoint},
        {"LeftPoint",leftPoint},
        {"BottomPoint",bottomPoint},
        {"RightPoint",rightPoint},
        {"TopPoint",topPoint},
    };

    updatePath(leftActiveArea,rightActiveArea,topActiveArea,bottomActiveArea);
    updateDraw();
    QPen pen(Qt::red);
    this->setEditPointF(editPoints);
    this->setCenterPointF(m_PropertyInfoMap["Position"].propertyValue.toPointF());
}
/**
 * @brief GY_Button::addItem 添加图形或容器
 * @param pItem
 * @return
 */
bool GY_Button::addItem(GY_Object *pItem)
{
    //隐藏全部
    pItem->getGraphicsItem()->hide();
    //若添加的单元为图形
//    if(pItem->inherits("GY_BasicsGraphicsObject"))
//    {
//        this->addToGroup((GY_BasicsGraphicsObject *)pItem);
//    }
//    //若添加的单元为容器
//    else if(pItem->inherits("GY_ContainerTransform"))
//    {
//        this->addToGroup((GY_ContainerTransform *)pItem);
//    }
    m_pTransFormGroup->addItem(pItem);
    m_ObjectItems.append(pItem);
    int numberOfStates = m_PropertyInfoMap["NumberOfStates"].propertyValue.value<int>();
    numberOfStates++;
    m_PropertyInfoMap["NumberOfStates"].propertyValue = numberOfStates;
    //插入所添加的图形或容器的父级编号于子映射中
//    m_ChildrensMap.insert(pItem->getGuid(),pItem);
    m_ObjectItems.at(currentIndex)->getGraphicsItem()->show();//显示当前图元
    //状态数量即为图元总数
    m_PropertyInfoMap["NumberOfStates"].propertyValue = m_ObjectItems.size();
    return true;
}

GY_Object *GY_Button::copyItem(const QString &guid)
{
    if(getUserData(GY_ENUM::IsCopy) == 1) {
//        return nullptr;
        GY_Object *tmp;
        this->copyItemSig(&tmp);
        //更新copyItem属性列表
        QMap<QString,GY_PropertyInfo>::Iterator iterProperty = m_PropertyInfoMap.begin();
        while (iterProperty != m_PropertyInfoMap.end()) {
            if(((iterProperty.value().updateType)&PointOfRelative) == PointOfRelative) {
                tmp->updateProperty(iterProperty.key(), submittingPropertyValues(iterProperty.value().updateType,iterProperty.value().propertyValue,this));
            }else if(((iterProperty.value().updateType)&NotUpdate) == NotUpdate){
                ++iterProperty;
                continue;
            }else {
                tmp->updateProperty(iterProperty.key(), iterProperty.value().propertyValue);
            }
            ++iterProperty;
        }
        return tmp;
    }
    GY_Object* pObj;
    if(guid.isEmpty()) {
        pObj = (GY_Object*)GY_GraphicsReflection::getInstance()->newInstanceObjectClass(this->metaObject()->className(),this->getGuid()+"-"+QString::number(m_MyGuid));
        m_MyGuid++;
    }else {
        pObj = (GY_Object*)GY_GraphicsReflection::getInstance()->newInstanceObjectClass(this->metaObject()->className(),guid);
    }
    if(pObj == nullptr) {
        qDebug()<<this->getGuid()<<"isEmpty";
        return nullptr;
    }
    QVector<GY_Object *> childrens = m_pTransFormGroup->getOrderChildrens();
    int childrensSize = childrens.size();
    for(int i=0;i<childrensSize;i++) {
//        if(childrens[i]->copyItem() == nullptr) {
//            qDebug()<<childrens[i]->getGuid()<<"isEmpty";
//            continue;
//        }
        ((GY_ContainerTransform*)pObj)->addItem(childrens[i]->copyItem());
    }

    QMap<QString,GY_PropertyInfo>::Iterator iterProperty = m_PropertyInfoMap.begin();
    while (iterProperty != m_PropertyInfoMap.end()) {
        if(((iterProperty.value().updateType)&PointOfRelative) == PointOfRelative) {
            pObj->updateProperty(iterProperty.key(), submittingPropertyValues(iterProperty.value().updateType,iterProperty.value().propertyValue,this));
        }else if(((iterProperty.value().updateType)&NotUpdate) == NotUpdate){
            ++iterProperty;
            continue;
        }else {
            pObj->updateProperty(iterProperty.key(), iterProperty.value().propertyValue);
        }
        ++iterProperty;
    }
    connect(pObj,&GY_Object::copyItemSig,this,&GY_Object::originalVersion);
    pObj->setUserData(GY_ENUM::IsCopy, QVariant::fromValue(1));
    return pObj;
}

bool GY_Button::removeItem(GY_Object *pItem)
{
    if(pItem->inherits("GY_BasicsGraphicsObject"))
    {
        this->removeFromGroup((GY_BasicsGraphicsObject *)pItem);
        m_ObjectItems.removeAt(currentIndex);
    }
    //若添加的单元为容器（item继承自GY_ContainerTransform）
    else if(pItem->inherits("GY_ContainerTransform"))
    {
        this->removeFromGroup((GY_ContainerTransform *)pItem);
        m_ObjectItems.removeAt(currentIndex);

        if(true)
        {
            int numberOfStates = m_PropertyInfoMap["NumberOfStates"].propertyValue.value<int>();
            numberOfStates--;
            m_PropertyInfoMap["NumberOfStates"].propertyValue = numberOfStates;
        }
    }
    //若不是上述两者
    else
    {
        return false;
    }

    //移除所删除的图形或容器的父级编号于子映射中
    m_ChildrensMap.remove(pItem->getGuid());
    //vector下标索引值currentIndex随删除而减小
    if(currentIndex >= m_ObjectItems.size())
    {
        currentIndex--;
    }
    m_PropertyInfoMap["NumberOfStates"].propertyValue = m_ObjectItems.size();

    return true;
}

bool GY_Button::updateIsVisible(const QString &, const QVariant &value)
{
    if(!value.canConvert<bool>())
    {
        return false;
    }
    bool isVisible = value.value<bool>();
    setVisible(isVisible);
    return true;
}

bool GY_Button::updatePoint(const QString &name, const QVariant &value)
{
    if(!value.canConvert<QPointF>())
    {
        return false;
    }
    QPointF mousePoint = this->mapFromScene(value.value<QPointF>());
    m_PropertyInfoMap[name].propertyValue = mousePoint;

    QPointF topRightPoint = m_PropertyInfoMap["TopRightPoint"].propertyValue.value<QPointF>();
    QPointF leftPoint = m_PropertyInfoMap["LeftPoint"].propertyValue.value<QPointF>();
    QPointF rightPoint = m_PropertyInfoMap["RightPoint"].propertyValue.value<QPointF>();
    QPointF topPoint = m_PropertyInfoMap["TopPoint"].propertyValue.value<QPointF>();
    QPointF bottomPoint = m_PropertyInfoMap["BottomPoint"].propertyValue.value<QPointF>();
    QPointF positionPoint = m_PropertyInfoMap["Position"].propertyValue.value<QPointF>();

    qreal leftActiveArea = leftPoint.x();
    qreal bottomActiveArea = bottomPoint.y();
    qreal rightActiveArea = rightPoint.x();
    qreal topActiveArea = topPoint.y();

    leftActiveArea = m_PropertyInfoMap["ActiveArea.Left"].propertyValue.toDouble();
    bottomActiveArea = m_PropertyInfoMap["ActiveArea.Bottom"].propertyValue.toDouble();
    rightActiveArea = m_PropertyInfoMap["ActiveArea.Right"].propertyValue.toDouble();
    topActiveArea = m_PropertyInfoMap["ActiveArea.Top"].propertyValue.toDouble();

    leftPoint.setX(leftActiveArea);
    leftPoint.setY((topPoint.y()+bottomPoint.y())/2);

    bottomPoint.setX((leftPoint.x()+rightPoint.x())/2);
    bottomPoint.setY(bottomActiveArea);

    rightPoint.setX(rightActiveArea);
    rightPoint.setY((topPoint.y()+bottomPoint.y())/2);

    topPoint.setX((leftPoint.x()+rightPoint.x())/2);
    topPoint.setY(topActiveArea);

    topRightPoint.setX(rightActiveArea);
    topRightPoint.setY(topActiveArea);

    if(name.endsWith("TopRightPoint"))
    {
        topRightPoint.setX(mousePoint.x());
        topRightPoint.setY(mousePoint.y());
        m_PropertyInfoMap["TopRightPoint"].propertyValue = topRightPoint;

        leftPoint.setY((mousePoint.y()+bottomPoint.y())/2);
        m_PropertyInfoMap["LeftPoint"].propertyValue = leftPoint;

        rightPoint.setX(topRightPoint.x());
        rightPoint.setY((mousePoint.y()+bottomPoint.y())/2);
        m_PropertyInfoMap["RightPoint"].propertyValue = rightPoint;

        topPoint.setX((rightPoint.x()+leftPoint.x())/2);
        topPoint.setY(topRightPoint.y());
        m_PropertyInfoMap["TopPoint"].propertyValue = topPoint;

        bottomPoint.setX(topPoint.x());
        m_PropertyInfoMap["BottomPoint"].propertyValue = bottomPoint;
    }
    else if (name.endsWith("LeftPoint"))
    {
        leftPoint.setX(mousePoint.x());
        leftPoint.setY((topPoint.y()+bottomPoint.y())/2);
        m_PropertyInfoMap["LeftPoint"].propertyValue = leftPoint;

        topPoint.setX((mousePoint.x()+rightPoint.x())/2);
        m_PropertyInfoMap["TopPoint"].propertyValue = topPoint;

        bottomPoint.setX((mousePoint.x()+rightPoint.x())/2);
        m_PropertyInfoMap["BottomPoint"].propertyValue = bottomPoint;

        leftActiveArea = mousePoint.x();
        m_PropertyInfoMap["ActiveArea.Left"].propertyValue = leftActiveArea;
    }
    else if (name.endsWith("RightPoint"))
    {
        rightPoint.setX(mousePoint.x());
        m_PropertyInfoMap["RightPoint"].propertyValue = rightPoint;

        topPoint.setX((mousePoint.x()+leftPoint.x())/2);
        m_PropertyInfoMap["TopPoint"].propertyValue = topPoint;

        bottomPoint.setX((mousePoint.x()+leftPoint.x())/2);
        m_PropertyInfoMap["BottomPoint"].propertyValue = bottomPoint;

        topRightPoint.setX(mousePoint.x());
        m_PropertyInfoMap["TopRightPoint"].propertyValue = topRightPoint;

        rightActiveArea = mousePoint.x();
        m_PropertyInfoMap["ActiveArea.Right"].propertyValue = rightActiveArea;

    }
    else if (name.endsWith("TopPoint"))
    {
        topPoint.setY(mousePoint.y());
        m_PropertyInfoMap["TopPoint"].propertyValue = topPoint;

        leftPoint.setY((mousePoint.y()+bottomPoint.y())/2);
        m_PropertyInfoMap["LeftPoint"].propertyValue = leftPoint;

        rightPoint.setY((mousePoint.y()+bottomPoint.y())/2);
        m_PropertyInfoMap["RightPoint"].propertyValue = rightPoint;

        topRightPoint.setY(mousePoint.y());
        m_PropertyInfoMap["TopRightPoint"].propertyValue = topRightPoint;

        topActiveArea = mousePoint.y();
        m_PropertyInfoMap["ActiveArea.Top"].propertyValue = topActiveArea;
    }
    else if (name.endsWith("BottomPoint"))
    {
        bottomPoint.setX((leftPoint.x()+rightPoint.x())/2);
        bottomPoint.setY(mousePoint.y());
        m_PropertyInfoMap["BottomPoint"].propertyValue = bottomPoint;

        leftPoint.setY((mousePoint.y()+topPoint.y())/2);
        m_PropertyInfoMap["LeftPoint"].propertyValue = leftPoint;

        rightPoint.setY((mousePoint.y()+topPoint.y())/2);
        m_PropertyInfoMap["RightPoint"].propertyValue = rightPoint;

        bottomActiveArea = mousePoint.y();
        m_PropertyInfoMap["ActiveArea.Bottom"].propertyValue = bottomActiveArea;
    }
    if (name.endsWith("ActiveArea.Left"))
    {
        leftPoint.setX(positionPoint.x()+leftActiveArea);
        m_PropertyInfoMap["LeftPoint"].propertyValue=leftPoint;
    }
    else if (name.endsWith("ActiveArea.Bottom"))
    {
        bottomPoint.setX(positionPoint.y()+bottomActiveArea);
        m_PropertyInfoMap["BottomPoint"].propertyValue=bottomPoint;
    }
    else if (name.endsWith("ActiveArea.Right"))
    {
        rightPoint.setX(positionPoint.x()+rightActiveArea);
        m_PropertyInfoMap["RightPoint"].propertyValue=rightPoint;
    }
    else if (name.endsWith("ActiveArea.Top"))
    {
        topPoint.setX(positionPoint.x()+topActiveArea);
        m_PropertyInfoMap["TopPoint"].propertyValue=topPoint;
    }

    updatePath(leftActiveArea,rightActiveArea,topActiveArea,bottomActiveArea);
    updateDraw();
    QMap<QVariant,QPointF> editPoint =
    {
        {"TopRightPoint",topRightPoint},
        {"LeftPoint",leftPoint},
        {"BottomPoint",bottomPoint},
        {"RightPoint",rightPoint},
        {"TopPoint",topPoint},
    };

    this->setEditPointF(editPoint);

    return true;
}

bool GY_Button::updatePosition(const QString &name, const QVariant &value)
{
    if(!value.canConvert<QPointF>())
    {
        return false;
    }
    m_PropertyInfoMap[name].propertyValue = mapFromScene(value.toPointF());
    updateDraw();
    this->setPos(value.toPointF());
    return true;
}

bool GY_Button::updateActiveAreaLeft(const QString &name, const QVariant &value)
{
    if(!value.canConvert<qreal>())
    {
        return false;
    }
    QPointF leftPoint = m_PropertyInfoMap["LeftPoint"].propertyValue.value<QPointF>();
    leftPoint.setX(value.toDouble());
    m_PropertyInfoMap["LeftPoint"].propertyValue = leftPoint;
    m_PropertyInfoMap[name].propertyValue = leftPoint.x();
    this->setEditPointF("LeftPoint",leftPoint);
    updateDraw();
    return true;
}

bool GY_Button::updateActiveAreaBottom(const QString &name, const QVariant &value)
{
    if(!value.canConvert<qreal>())
    {
        return false;
    }
    QPointF bottomPoint = m_PropertyInfoMap["BottomPoint"].propertyValue.value<QPointF>();
    bottomPoint.setY(value.toDouble());
    m_PropertyInfoMap["BottomPoint"].propertyValue = bottomPoint;
    m_PropertyInfoMap[name].propertyValue = bottomPoint.y();
    this->setEditPointF("BottomPoint",bottomPoint);
    updateDraw();
    return true;
}

bool GY_Button::updateActiveAreaRight(const QString &name, const QVariant &value)
{
    if(!value.canConvert<qreal>())
    {
        return false;
    }
    QPointF rightPoint = m_PropertyInfoMap["RightPoint"].propertyValue.value<QPointF>();
    rightPoint.setX(value.toDouble());
    m_PropertyInfoMap["RightPoint"].propertyValue = rightPoint;
    m_PropertyInfoMap[name].propertyValue = rightPoint.x();
    this->setEditPointF("RightPoint",rightPoint);
    updateDraw();
    return true;
}

bool GY_Button::updateActiveAreaTop(const QString &name, const QVariant &value)
{
    if(!value.canConvert<qreal>())
    {
        return false;
    }
    QPointF topPoint = m_PropertyInfoMap["TopPoint"].propertyValue.value<QPointF>();
    topPoint.setY(value.toDouble());
    m_PropertyInfoMap["TopPoint"].propertyValue = topPoint;
    m_PropertyInfoMap[name].propertyValue = topPoint.y();
    this->setEditPointF("TopPoint",topPoint);
    updateDraw();
    return true;
}

bool GY_Button::updateCurrentState(const QString &name, const QVariant &value)
{
    if(!value.canConvert<int>())
    {
        return false;
    }
    //若容器中未添加任何图元(button容器为空),更改CurrentState为任何整形值将不会发生变化
    if(m_ObjectItems.size()==0)
    {

        return true;
    }
    //如果currentState大于numberOfState,将默认从第一个图形开始
    if(value > m_PropertyInfoMap["NumberOfStates"].propertyValue.toInt())
    {
        m_ObjectItems.at(1)->getGraphicsItem()->show();
    }
    //如果currentState为0,将以不显示图形的状态作为初始状态（修复中）
    else if(value == 0)
    {
        for(int i=0;i<m_ObjectItems.size();i++)
        {
             m_ObjectItems[i]->getGraphicsItem()->hide();
        }
        m_ObjectItems[value.toInt()]->getGraphicsItem()->hide();
    }
    //如果currentState在正常范围内
    else
    {
        //隐藏当前索引对应的图元
        m_ObjectItems[currentIndex]->getGraphicsItem()->hide();
        //显示输入的值对应的图元
//        m_ObjectItems[value.toInt()-1]->getGraphicsItem()->show();
        m_ObjectItems[value.toInt()-1]->getGraphicsItem()->show();
        //更新当前索引值
//        currentIndex = value.toInt()-1;
        currentIndex = value.toInt()-1;
        m_PropertyInfoMap[name].propertyValue = value;
    //    qDebug()<<"CurrentState"<<m_PropertyInfoMap["CurrentState"].propertyValue<<currentIndex;
    }
    return true;
}

bool GY_Button::updateNumberOfStates(const QString &name, const QVariant &value)
{
    if(!value.canConvert<int>())
    {
        return false;
    }
    m_PropertyInfoMap[name].propertyValue = value;
    //若容器中未添加任何图元(button容器为空)
    if(m_ObjectItems.size()==0)
    {

        return true;
    }
    return true;
}

bool GY_Button::updateShowAllStates(const QString &name, const QVariant &value)
{
    if(!value.canConvert<bool>())
    {
        return false;
    }
    m_PropertyInfoMap[name].propertyValue = value;
    //若容器中未添加任何图元(button容器为空)
    if(m_ObjectItems.size()==0)
    {

        return true;
    }
    //如果为true则全部显示
    if(value == true)
    {
        for(int i=0;i<m_ObjectItems.size();i++)
        {
            if(m_ObjectItems[i]->inherits("GY_BasicsGraphicsObject") || m_ObjectItems[i]->inherits("GY_ContainerTransform"))
            {
                 m_ObjectItems[i]->getGraphicsItem()->show();
            }
        }
    }
    //如果为false则只显示当前索引表示的图形或容器
    else
    {
        //隐藏全部图元
        for(int i=0;i<m_ObjectItems.size();i++)
        {
            if(m_ObjectItems[i]->inherits("GY_BasicsGraphicsObject") || m_ObjectItems[i]->inherits("GY_ContainerTransform"))
            {
                 m_ObjectItems[i]->getGraphicsItem()->hide();
            }
        }
        //仅显示当前图元
        m_ObjectItems.at(currentIndex)->getGraphicsItem()->show();
        //判断当前索引下的数组成员是否显示
        if(currentIndex == 0)
        {
            //隐藏下标为0的图形
            m_ObjectItems.at(currentIndex)->getGraphicsItem()->hide();
            //每个数组元素后移一位
//            for(int i=m_ObjectItems.size();i>=0;i++)
//            {
//                m_ObjectItems[i+1] = m_ObjectItems[i];
//            }
//            m_ObjectItems.at(currentIndex)->getGraphicsItem()->show();
        }
    }

    return true;
}


void GY_Button::updatePath(const qreal &,const qreal &,const qreal &,const qreal &)
{
    QPointF topRightPoint = m_PropertyInfoMap["TopRightPoint"].propertyValue.value<QPointF>();//
    QPointF leftPoint = m_PropertyInfoMap["LeftPoint"].propertyValue.value<QPointF>();
    QPointF rightPoint = m_PropertyInfoMap["RightPoint"].propertyValue.value<QPointF>();
    QPointF topPoint = m_PropertyInfoMap["TopPoint"].propertyValue.value<QPointF>();
    QPointF bottomPoint = m_PropertyInfoMap["BottomPoint"].propertyValue.value<QPointF>();

    QPainterPath path;
    //给定左上角坐标、矩形宽高确定一个矩形
    QRectF rect = QRectF(topRightPoint.x(),topRightPoint.y(),leftPoint.x()-rightPoint.x(),bottomPoint.y()-topPoint.y());//有蓝色矩形

    path.addRect(rect);
    graphicsPathItem->setPath(path);
    this->addToGroup(graphicsPathItem);
    QPen pen(Qt::red);
    graphicsPathItem->setZValue(0.9);//设置容器激活区域的层级
    graphicsPathItem->setPen(pen);
    //若容器中存在图元且输入的当前索引大于0
    if(m_ObjectItems.size() > 0 && m_PropertyInfoMap["CurrentState"].propertyValue > 0)
        m_ObjectItems.at(currentIndex)->getGraphicsItem()->show();
    //若容器中存在图元且输入的当前索引等于0
    else if(m_ObjectItems.size() > 0 && m_PropertyInfoMap["CurrentState"].propertyValue == 0)
        m_ObjectItems.at(currentIndex)->getGraphicsItem()->hide();
}
/**
 * @brief GY_Button::updateDraw 矫正表示激活区域的矩形各边的位置
 */
void GY_Button::updateDraw()
{
    qreal leftActiveArea = m_PropertyInfoMap["ActiveArea.Left"].propertyValue.value<qreal>();
    qreal bottomActiveArea = m_PropertyInfoMap["ActiveArea.Bottom"].propertyValue.value<qreal>();
    qreal rightActiveArea = m_PropertyInfoMap["ActiveArea.Right"].propertyValue.value<qreal>();
    qreal topActiveArea = m_PropertyInfoMap["ActiveArea.Top"].propertyValue.value<qreal>();

    QPointF leftPoint = m_PropertyInfoMap["LeftPoint"].propertyValue.value<QPointF>();
    QPointF bottomPoint = m_PropertyInfoMap["BottomPoint"].propertyValue.value<QPointF>();
    QPointF rightPoint = m_PropertyInfoMap["RightPoint"].propertyValue.value<QPointF>();
    QPointF topPoint = m_PropertyInfoMap["TopPoint"].propertyValue.value<QPointF>();

    leftActiveArea = leftPoint.x();
    bottomActiveArea = bottomPoint.y();
    rightActiveArea = rightPoint.x();
    topActiveArea = topPoint.y();

    m_PropertyInfoMap["ActiveArea.Left"].propertyValue = leftActiveArea;
    m_PropertyInfoMap["ActiveArea.Bottom"].propertyValue = bottomActiveArea;
    m_PropertyInfoMap["ActiveArea.Right"].propertyValue = rightActiveArea;
    m_PropertyInfoMap["ActiveArea.Top"].propertyValue = topActiveArea;

    updatePath(leftActiveArea,rightActiveArea,topActiveArea,bottomActiveArea);
}

void GY_Button::initializationPropertyInfo()
{
    GY_PropertyInfo ObjectName(this);
    ObjectName.propertyName = "ObjectName";
    ObjectName.propertyValue = QString();
    ObjectName.updateType = Update|OrdinaryAttributes;
    ObjectName.propertyType = QStringPropertyType;
    ObjectName.updatePropertyFun = (GY_PropertyInfo::UpdatePropertyFun)&GY_Button::updateObjectName;
    m_PropertyInfoMap.insert("ObjectName",ObjectName);
    m_PropertyInfoMap["ObjectName"].setConnect();

    GY_PropertyInfo IsVisible(this);
    IsVisible.propertyName = "IsVisible";
    IsVisible.propertyValue = true;
    IsVisible.updateType = Update|OrdinaryAttributes;
    IsVisible.propertyType = BoolPropertyType;
    IsVisible.updatePropertyFun = (GY_PropertyInfo::UpdatePropertyFun)&GY_Button::updateIsVisible;
    m_PropertyInfoMap.insert("IsVisible",IsVisible);
    m_PropertyInfoMap["IsVisible"].setConnect();
    //位置
    GY_PropertyInfo p_Position(this);
    p_Position.propertyName = "Position";
    p_Position.propertyValue = QPointF(0,0);
    p_Position.updateType = Update|MoveUpdate|PointOfRelative;
    p_Position.propertyType = PointPropertyType;
    p_Position.updatePropertyFun = (GY_PropertyInfo::UpdatePropertyFun)&GY_Button::updatePosition;
    m_PropertyInfoMap.insert(p_Position.propertyName,p_Position);
    m_PropertyInfoMap["Position"].setConnect();
    //激活区域（左）
    GY_PropertyInfo p_ActiveAreaLeft(this);
    p_ActiveAreaLeft.propertyName = "ActiveArea.Left";
    p_ActiveAreaLeft.propertyValue = qreal(-60.0);
    p_ActiveAreaLeft.updateType = Update|OrdinaryAttributes;
    p_ActiveAreaLeft.propertyType = QrealPropertyType;
    p_ActiveAreaLeft.updatePropertyFun = (GY_PropertyInfo::UpdatePropertyFun)&GY_Button::updateActiveAreaLeft;
    m_PropertyInfoMap.insert(p_ActiveAreaLeft.propertyName,p_ActiveAreaLeft);
    m_PropertyInfoMap["ActiveArea.Left"].setConnect();
    //激活区域（下）
    GY_PropertyInfo p_ActiveAreaBottom(this);
    p_ActiveAreaBottom.propertyName = "ActiveArea.Bottom";
    p_ActiveAreaBottom.propertyValue = qreal(-30.0);
    p_ActiveAreaBottom.updateType = Update|OrdinaryAttributes;
    p_ActiveAreaBottom.propertyType = QrealPropertyType;
    p_ActiveAreaBottom.updatePropertyFun = (GY_PropertyInfo::UpdatePropertyFun)&GY_Button::updateActiveAreaBottom;
    m_PropertyInfoMap.insert(p_ActiveAreaBottom.propertyName,p_ActiveAreaBottom);
    m_PropertyInfoMap["ActiveArea.Bottom"].setConnect();
    //激活区域（右）
    GY_PropertyInfo p_ActiveAreaRight(this);
    p_ActiveAreaRight.propertyName = "ActiveArea.Right";
    p_ActiveAreaRight.propertyValue = qreal(60.0);
    p_ActiveAreaRight.updateType = Update|OrdinaryAttributes;
    p_ActiveAreaRight.propertyType = QrealPropertyType;
    p_ActiveAreaRight.updatePropertyFun = (GY_PropertyInfo::UpdatePropertyFun)&GY_Button::updateActiveAreaRight;
    m_PropertyInfoMap.insert(p_ActiveAreaRight.propertyName,p_ActiveAreaRight);
    m_PropertyInfoMap["ActiveArea.Right"].setConnect();
    //激活区域（上）
    GY_PropertyInfo p_ActiveAreaTop(this);
    p_ActiveAreaTop.propertyName = "ActiveArea.Top";
    p_ActiveAreaTop.propertyValue = qreal(30.0);
    p_ActiveAreaTop.updateType = Update|OrdinaryAttributes;
    p_ActiveAreaTop.propertyType = QrealPropertyType;
    p_ActiveAreaTop.updatePropertyFun = (GY_PropertyInfo::UpdatePropertyFun)&GY_Button::updateActiveAreaTop;
    m_PropertyInfoMap.insert(p_ActiveAreaTop.propertyName,p_ActiveAreaTop);
    m_PropertyInfoMap["ActiveArea.Top"].setConnect();
    //当前状态
    GY_PropertyInfo p_CurrentState(this);
    p_CurrentState.propertyName = "CurrentState";
    p_CurrentState.propertyValue = int(1);
    p_CurrentState.updateType = Update|OrdinaryAttributes;
    p_CurrentState.propertyType = IntPropertyType;
    p_CurrentState.updatePropertyFun = (GY_PropertyInfo::UpdatePropertyFun)&GY_Button::updateCurrentState;
    m_PropertyInfoMap.insert(p_CurrentState.propertyName,p_CurrentState);
    m_PropertyInfoMap["CurrentState"].setConnect();
    //状态数
    GY_PropertyInfo p_NumberOfStates(this);
    p_NumberOfStates.propertyName = "NumberOfStates";
    p_NumberOfStates.propertyValue = int(2);
    p_NumberOfStates.updateType = Update|OrdinaryAttributes;
    p_NumberOfStates.propertyType = IntPropertyType;
    p_NumberOfStates.updatePropertyFun = (GY_PropertyInfo::UpdatePropertyFun)&GY_Button::updateNumberOfStates;
    m_PropertyInfoMap.insert(p_NumberOfStates.propertyName,p_NumberOfStates);
    m_PropertyInfoMap["NumberOfStates"].setConnect();
    //是否显示所有状态
    GY_PropertyInfo p_ShowAllStates(this);
    p_ShowAllStates.propertyName = "ShowAllStates";
    p_ShowAllStates.propertyValue = bool(false);
    p_ShowAllStates.updateType = Update|OrdinaryAttributes;
    p_ShowAllStates.propertyType = BoolPropertyType;
    p_ShowAllStates.updatePropertyFun = (GY_PropertyInfo::UpdatePropertyFun)&GY_Button::updateShowAllStates;
    m_PropertyInfoMap.insert(p_ShowAllStates.propertyName,p_ShowAllStates);
    m_PropertyInfoMap["ShowAllStates"].setConnect();
    /******************************************附加属性******************************************/
    //宽高拉伸点
    GY_PropertyInfo TopRightPoint(this);
    TopRightPoint.propertyName="TopRightPoint";
    TopRightPoint.propertyValue=QPointF(60.0,30.0);
    TopRightPoint.updateType = NotUpdate;
    TopRightPoint.propertyType = PointPropertyType;
    TopRightPoint.updatePropertyFun=(GY_PropertyInfo::UpdatePropertyFun)&GY_Button::updatePoint;
    m_PropertyInfoMap.insert(TopRightPoint.propertyName,TopRightPoint);
    m_PropertyInfoMap["TopRightPoint"].setConnect();
    //左拉伸点
    GY_PropertyInfo leftPoint(this);
    leftPoint.propertyName="LeftPoint";
    leftPoint.propertyValue=QPointF(-60.0,0.0);
    leftPoint.updateType = NotUpdate;
    leftPoint.propertyType = PointPropertyType;
    leftPoint.updatePropertyFun=(GY_PropertyInfo::UpdatePropertyFun)&GY_Button::updatePoint;
    m_PropertyInfoMap.insert(leftPoint.propertyName,leftPoint);
    m_PropertyInfoMap["LeftPoint"].setConnect();
    //右拉伸点
    GY_PropertyInfo rightPoint(this);
    rightPoint.propertyName="RightPoint";
    rightPoint.propertyValue=QPointF(60.0,0.0);
    rightPoint.updateType = NotUpdate;
    rightPoint.propertyType = PointPropertyType;
    rightPoint.updatePropertyFun=(GY_PropertyInfo::UpdatePropertyFun)&GY_Button::updatePoint;
    m_PropertyInfoMap.insert(rightPoint.propertyName,rightPoint);
    m_PropertyInfoMap["RightPoint"].setConnect();
    //上拉伸点
    GY_PropertyInfo topPoint(this);
    topPoint.propertyName="TopPoint";
    topPoint.propertyValue=QPointF(0.0,30.0);
    topPoint.updateType = NotUpdate;
    topPoint.propertyType = PointPropertyType;
    topPoint.updatePropertyFun=(GY_PropertyInfo::UpdatePropertyFun)&GY_Button::updatePoint;
    m_PropertyInfoMap.insert(topPoint.propertyName,topPoint);
    m_PropertyInfoMap["TopPoint"].setConnect();
    //下拉伸点
    GY_PropertyInfo bottomPoint(this);
    bottomPoint.propertyName="BottomPoint";
    bottomPoint.propertyValue=QPointF(0.0,-30.0);
    bottomPoint.updateType = NotUpdate;
    bottomPoint.propertyType = PointPropertyType;
    bottomPoint.updatePropertyFun=(GY_PropertyInfo::UpdatePropertyFun)&GY_Button::updatePoint;
    m_PropertyInfoMap.insert(bottomPoint.propertyName,bottomPoint);
    m_PropertyInfoMap["BottomPoint"].setConnect();
}

void GY_Button::recvDoubleClicked()
{
    //若容器中未添加任何图元(button容器为空)
    if(m_ObjectItems.size()==0)
    {
        m_ObjectItems.clear();
        currentIndex = 0;
    }
    else
    {
        if(m_PropertyInfoMap["ShowAllStates"].propertyValue == false)
        {

            //隐藏前一个索引所代表的图形或容器
            m_ObjectItems.at(currentIndex)->getGraphicsItem()->hide();
        }
        else
        {
            //显示所有的图形或容器且不再随双击事件切换隐藏但索引值能够随双击事件依次变化
            for(int i=0;i<m_ObjectItems.size();i++)
            {
                m_ObjectItems[i]->getGraphicsItem()->show();
            }
        }
        //每双击一次切换至下一个索引，无论ShowAllStates是否为true/false
        currentIndex++;
        //当索引大于等于当前图元范围时索引归零
        if(currentIndex >= m_ObjectItems.size())
        {
            currentIndex = 0;
        }
        //更新当前状态编号（注意数组是从第0个开始）
        m_PropertyInfoMap["CurrentState"].propertyValue = currentIndex+1;
        //    qDebug()<<"recvDoubleClicked CurrentState"<<m_PropertyInfoMap["CurrentState"].propertyValue<<currentIndex;
        //显示当前索引所代表的图形或容器
        m_ObjectItems.at(currentIndex)->getGraphicsItem()->show();
    }
}
